﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;
using WHC.Security.Entity;

namespace WHC.Hotel.UI.Report
{
    public partial class KTVCheckoutReport : BaseForm
    {
        private decimal cash = 0M;//现金
        private decimal card = 0M;//储值卡
        private decimal daijin = 0M;//代金劵
        private decimal national = 0M;//国内信用卡
        private decimal interCredit = 0M;//国际信用卡
        private decimal deposit = 0M;//账面押金

        public KTVCheckoutReport()
        {
            InitializeComponent();
            InitDictItem();
        }

        private void ResetZero()
        {
            cash = 0M;//现金
            card = 0M;//储值卡
            daijin = 0M;//代金劵
            national = 0M;//国内信用卡
            interCredit = 0M;//国际信用卡
            deposit = 0M;//账面押金
        }

        private void InitDictItem()
        {
            this.txtPayType.Items.Clear();
            this.txtPayType.Items.AddRange(DictItemUtil.GetPayType());
            if (this.txtPayType.Items.Count > 0)
            {
                this.txtPayType.SelectedIndex = 0;
            }

            this.txtCreator.Items.Clear();
            List<UserInfo> userList = WHC.Security.BLL.BLLFactory<WHC.Security.BLL.User>.Instance.GetAll();
            foreach (UserInfo info in userList)
            {
                this.txtCreator.Items.Add(info.FullName);
            }
            this.txtCreator.Items.Insert(0, "所有收银人");
        }

        private void KFCheckoutReport_Load(object sender, EventArgs e)
        {
            this.winGridView1.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridView1.AppendedMenu = this.contextMenuStrip1;
            this.winGridView1.dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);

            this.dtStart.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            this.dtEnd.Value = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));
        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            ResetZero();
            DataGridViewRow row;
            for (int i = 0; i < winGridView1.dataGridView1.Rows.Count; i++)
            {
                row = winGridView1.dataGridView1.Rows[i];
                cash += Convert.ToDecimal(row.Cells["Cash"].Value);
                card += Convert.ToDecimal(row.Cells["Card"].Value);
                daijin += Convert.ToDecimal(row.Cells["DaijinTicket"].Value);
                national += Convert.ToDecimal(row.Cells["NationalCredit"].Value);
                interCredit += Convert.ToDecimal(row.Cells["InterCredit"].Value);
                deposit += Convert.ToDecimal(row.Cells["Deposit"].Value);
            }

            this.lblCash.Text = cash.ToString("C2");
            this.lblCard.Text = card.ToString("C2");
            this.lblDaijin.Text = daijin.ToString("C2");
            this.lblDeposit.Text = deposit.ToString("C2");
            this.lblNational.Text = national.ToString("C2");
            this.lblInterCredit.Text = interCredit.ToString("C2");
        }

        private void BinData()
        {
            //BillNumber, RoomNo, CustomerName, Deposit, Consumption, OfferMoney, PayMoney, KeepChange, Creator, 
            //PayType, Cash,Card,DaijinTicket, NationalCredit,InterCredit, case Invoice when '1' then '是' else '否' end as Invoice,
            //CreateDate,InHotelDate,CheckStatus,(CreditCardNumber+ InterCreditCardNumber) CreditCard,Note

            this.lblCreator.Text = this.txtCreator.Text;

            #region 添加别名解析
            this.winGridView1.AddColumnAlias("BillNumber", "结账单号");
            this.winGridView1.AddColumnAlias("RoomNo", "房间号");
            this.winGridView1.AddColumnAlias("Deposit", "押金");
            this.winGridView1.AddColumnAlias("Consumption", "消费金额");
            this.winGridView1.AddColumnAlias("OfferMoney", "优惠金额");
            this.winGridView1.AddColumnAlias("PayMoney", "实收金额");
            this.winGridView1.AddColumnAlias("KeepChange", "找零");
            this.winGridView1.AddColumnAlias("Creator", "收款人");
            this.winGridView1.AddColumnAlias("PayType", "收款方式");
            this.winGridView1.AddColumnAlias("Cash", "现金");
            this.winGridView1.AddColumnAlias("Card", "储值卡");
            this.winGridView1.AddColumnAlias("DaijinTicket", "代金劵");
            this.winGridView1.AddColumnAlias("NationalCredit", "国内信用卡");
            this.winGridView1.AddColumnAlias("InterCredit", "国际信用卡");
            this.winGridView1.AddColumnAlias("Invoice", "是否开发票");
            this.winGridView1.AddColumnAlias("CreateDate", "收款时间");
            this.winGridView1.AddColumnAlias("InHotelDate", "进店时间");
            this.winGridView1.AddColumnAlias("CheckStatus", "结账状态");
            this.winGridView1.AddColumnAlias("CreditCard", "信用卡号");
            this.winGridView1.AddColumnAlias("Note", "结账备注");
            #endregion

            SearchCondition condition = new SearchCondition();
            condition.AddCondition("CreateDate", this.dtStart.Value.ToString(), SqlOperator.MoreThanOrEqual)
                .AddCondition("CreateDate", this.dtEnd.Value.ToString(), SqlOperator.LessThanOrEqual)
                .AddCondition("PayType", this.txtPayType.Text, SqlOperator.Like) ;
            if (string.IsNullOrEmpty(this.txtCreator.Text) || this.txtCreator.Text == "所有收银人")
            {
                this.lblCreator.Text = "所有收银人";
            }
            else
            {
                condition.AddCondition("Creator", this.txtCreator.Text, SqlOperator.Like);
            }
            string filter = condition.BuildConditionSql(DatabaseType.SqlServer);

            DataTable dt = BLLFactory<KTVSuiteBill>.Instance.GetCheckOutReport(filter);            
            this.winGridView1.DataSource = dt.DefaultView;
            this.winGridView1.PrintTitle = Portal.gc.gAppUnit + " -- " + "KTV收银明细查询报表";
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BinData();
        }

        private void menu_InRefresh_Click(object sender, EventArgs e)
        {
            BinData();
        }

        private void txtPayType_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                BinData();
            }
        }

        private void txtCreator_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                BinData();
            }
        }

    }
}
